package com.yeyks.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.AdminAppInfoDTO;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.param.QuerySetVersionParam;
import com.yeyks.common.appcommon.param.account.ForgetPwdParam;
import com.yeyks.common.appcommon.param.account.UpdatePwdParam;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.em.SmsCodeTypeEnum;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.append.sms.SmsService;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.config.service.AdminAppInfoService;
import com.yeyks.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import static com.yeyks.common.constants.SysLogMsg.ACCOUNT_UPDATE_PWD;

/**
 * @author Administrator
 */
@Api(tags = "AppSettingController_设置接口", description = "设置相关接口[修改密码. 查看版本]等")
@RestController
@RequestMapping("/admin/setting")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppSettingController extends BaseController {


    private final AccountInfoService accountInfoService;

    private final AdminAppInfoService adminAppInfoService;

    private final SmsService smsService;

    /**
     * 修改密码
     */
    @AdminAppLog(value = ACCOUNT_UPDATE_PWD)
    @ApiOperation("修改密码")
    @PostMapping("/pwd")
    public CommonResult updatePwd(@Valid UpdatePwdParam updatePwdParam){


        AppAccountDTO appAccountDTO = validToken(updatePwdParam.getToken(),
                updatePwdParam.getVersion(), updatePwdParam.getPlatform());

        return accountInfoService.updatePwd(updatePwdParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appAccountInfoService.updatePwd(updatePwdParam);
    }


    @ApiOperation("忘记密码发送短信")
    @PostMapping("/send/code")
    public CommonResult sendPhoneCode(@RequestParam(value = "username", required = true) String username) {

        //验证账户名称
        AccountInfo accountInfo = accountInfoService.getOne(new QueryWrapper<AccountInfo>().eq(AccountInfo.USERNAME, username));
        if(null == accountInfo){
            return CommonResult.failed("该账户不存在");
        }else if(StringUtils.isEmpty(accountInfo.getPhone())){
            return CommonResult.failed("该账户没有绑定手机号");
        }
        Boolean flag = smsService.sendSmsForIncomeAccount(accountInfo.getPhone(), SmsCodeTypeEnum.FORGET_PWD_ADMIN_APP.value());
        if (flag) {
            return CommonResult.success(null, "验证码发送成功");
        }
        return CommonResult.failed("发送验证码失败");
    }

    /**
     * 忘记密码
     */
    @ApiOperation("忘记密码")
    @PostMapping("/forget/pwd")
    public CommonResult forgetPwd(@Valid ForgetPwdParam forgetPwdParam){

        AccountInfo accountInfo = accountInfoService.getOne(new QueryWrapper<AccountInfo>().eq(AccountInfo.USERNAME,
                forgetPwdParam.getUsername()));
        //判断账户名是否存在
        if (null == accountInfo) {
            return CommonResult.failed("账户名不存在");
        }
        //TODO 校验验证码
        if (!smsService.verifySmsForSignup(accountInfo.getPhone(), forgetPwdParam.getCode())) {
            return CommonResult.failed("验证码输入有误");
        }
        //判断两次密码是否相同
        if(!forgetPwdParam.getNewPwd().equals(forgetPwdParam.getConfirmPwd())){
            return CommonResult.failed("两次密码不同");
        }

        return accountInfoService.forgetPwd(forgetPwdParam, accountInfo);
    }


    /**
     * 查看版本
     */
    @ApiOperation("查看版本")
    @PostMapping("/get/version")
    public CommonResult<AdminAppInfoDTO> getVersion(@Valid QuerySetVersionParam querySetVersionParam){

        return adminAppInfoService.getVersion(querySetVersionParam);
    }
}
